package com.ruoyi.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.Result;
import com.ruoyi.common.ResultObject;
import com.ruoyi.utils.JwtUtils;
import com.ruoyi.mapper.IMerchantUserMapper;
import com.ruoyi.userlogin.dto.UserEntityDTO;
import com.ruoyi.userlogin.pojo.UserEntity;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 26352
 * @Date 2023/2/24 18:20
 * @Description TODO
 * @Version 1.0
 */
@Service
public class IMerchantUserService {
    @Resource
    private IMerchantUserMapper iMerchantUserMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    ServerHttpRequest request;



    /**
     * 状态审核
     * @param
     * @return
     */

    public Result upadtastutas(UserEntityDTO userEntityDTO) {
        if (userEntityDTO.getUserId()!=null && userEntityDTO.getStatus().intValue()==1){

            UserEntity userEntity = new UserEntity();
            userEntity.setUserId(userEntityDTO.getUserId());
            userEntity.setStatus(userEntityDTO.getStatus());

            iMerchantUserMapper.updateById(userEntity);

            return new  Result(true,"审核通过",null);
        }
        if (userEntityDTO.getUserId()!=null && userEntityDTO.getStatus().intValue()==2){

            UserEntity userEntity = new UserEntity();
            userEntity.setUserId(userEntityDTO.getUserId());
            userEntity.setStatus(userEntityDTO.getStatus());

            iMerchantUserMapper.updateById(userEntity);
            return new  Result(true,"审核不通过",null);
        }
        return new  Result(false,"审核失败",null);
    }

    /**
     * 查询未审核的列表
     */
    public ResultObject<UserEntity> findlist() {
        QueryWrapper<UserEntity> queryWrapper =new QueryWrapper();
        queryWrapper.lambda().eq(UserEntity::getStatus,0);
        List<UserEntity> merchantUsers = iMerchantUserMapper.selectList(queryWrapper);
        return new  ResultObject(true,"1","查询成功",merchantUsers);

    }


    /**
     * 登录方法
     */
    public ResultObject<Map> login(UserEntityDTO userEntityDTO) {

        //构造查询条件（前台传来的用户信息）
        QueryWrapper<UserEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(UserEntity::getUsername,userEntityDTO.getUsername());

        //根据查询条件查到数据
        List<UserEntity> loginEntities = iMerchantUserMapper.selectList(queryWrapper);

        //判断是否有这个用户
        if (loginEntities!=null && loginEntities.size()>0){

            //获取到数据库查询到的数据
            UserEntity loginEntity1 = loginEntities.get(0);

            //查询是否审核成功
            if (loginEntity1.getStatus().intValue()!=1){
                //审核不通过 返回提示信息
                return new ResultObject<>(false,"0","请先联系管理员审核账号",null);
            }

            //比对密码是否正确(调用 BCryptPasswordEncoder)明文在前 密文在后
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            if (!bCryptPasswordEncoder.matches(userEntityDTO.getPassword(),loginEntity1.getPassword())){

                //密码错误 返回提示信息
                return new ResultObject<>(false,"0","用户名或密码错误",null);

            }

            Map map =new HashMap();
            map.put("uid",loginEntity1.getUserId());
            map.put("uname",loginEntity1.getUsername());

            //生成token令牌
            String token = JwtUtils.createToken(loginEntity1.getUserId().toString(), loginEntity1.getUsername(), map);

            //令牌（存到redis中 2分钟有效期）
            redisTemplate.opsForValue().set("token_"+loginEntity1.getUserId(),token,2, TimeUnit.MINUTES);

            //登录成功
            return new ResultObject<>(true,"1",token,map);

        } else {

            //用户名错误 返回提示信息
            return new ResultObject<>(false,"0","用户名或密码错误",null);

        }
    }
}
